package com.sk.myview;


import java.io.File;

import com.sk.water.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class MyPopupWindow extends PopupWindow{
	private Activity context;
	private View popupView;
	private Button btn_take_photo, btn_pick_photo, btn_cancel;
	private String filePath;
	public MyPopupWindow(Context context,String filePath){
		super(context);
		this.context = (Activity)context;
		this.filePath = filePath;
		popupView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.chose_picture,null);
		ClickEvent clickEvent = new ClickEvent();
				
		btn_take_photo = (Button) popupView.findViewById(R.id.btn_take_photo);
		btn_pick_photo = (Button) popupView.findViewById(R.id.btn_pick_photo);
		btn_cancel = (Button) popupView.findViewById(R.id.btn_cancel);
		
		btn_cancel.setOnClickListener(clickEvent);
		btn_pick_photo.setOnClickListener(clickEvent);
		btn_take_photo.setOnClickListener(clickEvent);
		
		
		this.setContentView(popupView);
		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setAnimationStyle(R.style.PopupAnimation);
		this.setFocusable(true);
		ColorDrawable dw = new ColorDrawable(0xb0000000);
		this.setBackgroundDrawable(dw);
		popupView.setOnTouchListener(new View.OnTouchListener() {			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int height = popupView.findViewById(R.id.pop_layout).getTop();
				int y=(int) event.getY();
				if(event.getAction()==MotionEvent.ACTION_UP){
					if(y<height){
						dismiss();
					}
				}				
				return false;
			}
		});
		
	}
	
	public MyPopupWindow(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
	
	private class ClickEvent implements OnClickListener{
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_take_photo:
				Intent takePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				takePhoto.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath)));
				context.startActivityForResult(takePhoto, 0);
				dismiss();
				break;
			case R.id.btn_pick_photo:
				Intent getLocalImages = new Intent(Intent.ACTION_GET_CONTENT);
				getLocalImages.addCategory(Intent.CATEGORY_OPENABLE);
				getLocalImages.setType("image/*");
				context.startActivityForResult(getLocalImages, 1);
				dismiss();
				break;
			case R.id.btn_cancel:
				dismiss();
				break;

			default:
				break;
			}
			
		}
	}
}
